/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.aapl.registry;

import org.apache.ibatis.aapl.util.ReflectUtil;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 一个根据SQL id（即namespace+方法名称的id）获取到相应的SqlMethod的类
 *
 * @author gu
 * @date 2020/3/26 16:32
 */
public class MethodRegistry {

  private Map<String, SqlMethod> methodRegistry = new HashMap<>(256);

  private static MethodRegistry registry = new MethodRegistry();

  private MethodRegistry() {
  }

  public static MethodRegistry getMethodRegistry() {
    return registry;
  }

  public SqlMethod getMethod(String id) {
    return this.methodRegistry.get(id);
  }

  public void putMethod(MappedStatement mappedStatement) {
    String id = mappedStatement.getId();
    SqlCommandType type = mappedStatement.getSqlCommandType();
    Method m = ReflectUtil.getMethodByFullName(id);
    if (type == null || m == null) {
      return;
    }
    SqlMethod method = new SqlMethod(m, type);
    methodRegistry.put(id, method);
  }


  public SqlMethod getMethodByStatement(MappedStatement statement) {
    SqlMethod method = getMethod(statement.getId());
    if (method == null) {
      putMethod(statement);
    }
    return getMethod(statement.getId());
  }

  public MappedStatement getMappedStatement(Object handler) {
    if (handler instanceof DefaultResultSetHandler) {
      // 什么都不做
    } else if (handler instanceof DefaultParameterHandler) {
      // 什么都不做
    } else {
      return null;
    }
    try {
      Field mappedStatement = handler.getClass().getDeclaredField("mappedStatement");
      mappedStatement.setAccessible(true);
      Object o = mappedStatement.get(handler);
      return (MappedStatement) o;
    } catch (NoSuchFieldException | IllegalAccessException e) {
      e.printStackTrace();
    }
    return null;
  }

  public SqlMethod getMethod(DefaultParameterHandler handler) {
    MappedStatement mappedStatement = getMappedStatement(handler);
    if (mappedStatement == null) {
      return null;
    } else {
      return getMethodByStatement(mappedStatement);
    }
  }

  public SqlMethod getMethod(DefaultResultSetHandler handler) {
    MappedStatement mappedStatement = getMappedStatement(handler);
    if (mappedStatement == null) {
      return null;
    } else {
      return getMethodByStatement(mappedStatement);
    }
  }
}
